/**
 * 黑屏
 */
define(['jquery', '_global', '_cartData', '_queryConditions'], function ($, _gl, _CD, _qc) {
    var lockScreenStartTime = 'lockScreenStartTime';
    var lockScrenEventTime = 'lockScrenEventTime';

    function getStartTime(time) {
        return parseInt(window.localStorage.getItem(lockScreenStartTime));
    }

    function setStartTime(time) {
        window.localStorage.setItem(lockScreenStartTime, time);
    }

    function getEventTime() {
        return parseInt(window.localStorage.getItem(lockScrenEventTime));
    }

    function setEventTime(time) {
        window.localStorage.setItem(lockScrenEventTime, time);
    }

    function createLock() {
        var lockDiv = document.createElement('div');
        lockDiv.style.display = 'none';
        lockDiv.style.position = 'fixed';
        lockDiv.style.left = 0;
        lockDiv.style.top = 0;
        lockDiv.style.bottom = 0;
        lockDiv.style.width = '100%';
        lockDiv.style.background = 'url('+BASEPATH+'assets/images/pingbao.jpg?'+(new Date().getTime())+') no-repeat center';
        lockDiv.style.zIndex = 2147483647;
        lockDiv.id = '_lockScreen';
        document.body.appendChild(lockDiv);
    }

    function _lockScreen() {
        if (!!window.lockScreenTimer) {
            clearInterval(window.lockScreenTimer)
        }
        console.log("屏幕载入时间"+ (new Date()))
        createLock();
        var lockScreenEl = document.getElementById("_lockScreen");
        var startTime = (new Date()).getTime();
        var goTimer = 0; // 已经监听的时间
        var lockTime = 300; //监听最长时间 20min 处理黑屏
       // lockTime = 5;
        var refreshStartTime = startTime;
        var refreshTime =  2400000;//1 * 60 * 60 * 1000; // 刷新页面的时间
       // refreshTime = 2 * 10 * 1000;
        var islockScreenEvent = true;
        
        setStartTime(startTime);
        setEventTime(startTime);
        // 1s 监控一次
        window.lockScreenTimer = setInterval(function () {
            if (getEventTime() == getStartTime() && goTimer >= lockTime) {
                clearInterval(window.lockScreenTimer);
                window.location.href = "/Home/Main"; //屏幕解锁以后的链接跳转
             //   lockScreenEl.style.background = 'url(' + BASEPATH + 'assets/images/pingbao.jpg?' + startTime + ') no-repeat center';
             //   lockScreenEl.style.display = 'block';
                goTimer = 0; // 初始化已经监听的时间
            }
            goTimer++;
        }, 1000);

        var eventFunc = function (e) {
            if (islockScreenEvent) {      
                clearInterval(window.lockScreenTimer);
                islockScreenEvent = false;
                //console.log("距离最近一次操作屏幕的时间：" + (new Date(getEventTime())));
                startTime = (new Date()).getTime();
                setEventTime(startTime);
                //console.log("当前操作屏幕的时间：" + (new Date(getEventTime())))
                if (lockScreenEl.style.display == 'block') {
                    //console.log("判断是否应该刷新前的时间" + (new Date()));
                    if (Math.abs(getEventTime() - refreshStartTime) >= refreshTime) {
                        //console.log("刷新页面的操作，上一步的时间：" + (new Date()));
                        lockScreenEl.style.display = 'none';
                        _CD.clear();
                        _qc.removeNetPoint();
                        var $netPointNameShow = $("#netPointNameShow");
                        $netPointNameShow.html($netPointNameShow.attr("data-name"));
                        $("#netPointAddressShow").html($netPointNameShow.attr("data-address"));
                        var href = window.location.href.indexOf("#") > 0 ? window.location.href.split("#")[0] : window.location.href;
                        window.location.href = href + '#goods/0';
                    } else {
                        //console.log("解锁屏幕的操作，上一步的时间：" + (new Date()));
                        lockScreenEl.style.display = 'none';
                        window.location.href = "/Home/Main";
                    }
                }
            //    window.location.href = "/Home/Main";
                goTimer = 0;
                setStartTime(startTime);
                window.lockScreenTimer = setInterval(function () {
                    if (getEventTime() == getStartTime() && goTimer >= lockTime) {
                        clearInterval(window.lockScreenTimer);
                        window.location.href = "/Home/Main"; //屏幕解锁以后的链接跳转
                     //   lockScreenEl.style.background = 'url(' + BASEPATH + 'assets/images/pingbao.jpg?' + startTime + ') no-repeat center';
                     //   lockScreenEl.style.display = 'block';
                        goTimer = 0; // 初始化已经监听的时间
                    }
                    goTimer++;
                }, 1000);
                islockScreenEvent = true;
                if (e.target.id == '_lockScreen') {
                    e.preventDefault();
                }
            }
        };
       
        if (typeof (window.ontouchstart) !== 'undefined') {
            //$(document).off("touchstart").on("touchstart")
            document.removeEventListener('touchstart', eventFunc);
            document.addEventListener('touchstart', eventFunc);
        } else {
            document.removeEventListener('mousedown', eventFunc);
            document.addEventListener('mousedown', eventFunc);
            document.removeEventListener('keydown', eventFunc);
            document.addEventListener('keydown', eventFunc);
        }

    }

    return {
        start: _lockScreen
    }
});